
<script>
    $(function(){
        check();
        $(".cbt").on('click',function(){
            trace('点击了');
            if($(this).hasClass("cb_active")){
                $(this).removeClass("cb_active");
            }else{
                $(this).addClass("cb_active");
            }
            check(this);
        });
        function check(that){
            var checks = $(".cbt");
            if($(that).hasClass("all")){
                if($(that).hasClass("cb_active")){
                    for(var i = 0; i < checks.length; i++){
                        $(checks[i]).addClass("cb_active");
                    }
                }else{
                    for(var i = 0; i < checks.length; i++){
                        $(checks[i]).removeClass("cb_active");
                    }
                }
            }
            var all = 1;
            for(var i = 0; i < checks.length; i++){
                console.log('循环 i= '+i);
                if($(checks[i]).hasClass("cb_active")){
                    console.log('取消选中 i= '+i);
                    var input = $(checks[i]).children("input");
                    if(input){
                        $(input).prop("checked","checked");
                    }
                }else{
                    console.log('取消选中');
                    var input = $(checks[i]).children("input");
                    console.log(checks[i]);
                    console.log(input);
                    if(input){
                        console.log('取消');
                        $(input).removeProp("checked");
                    }
                    if(!$(checks[i]).hasClass("all")){
                        all = 0;
                    }
                }
            }
            console.log("all= "+all);
            if(all == 0){
                $(".cbt.all").removeClass("cb_active");
            }else{
                $(".cbt.all").addClass("cb_active");
            }
        }
    });
</script>
